/*
 * This file is a part of the RMI Plugin for Eclipse tutorials.
 * Copyright (C) 2002-7 Genady Beryozkin
 */
package oldVersion.server;

/**
 *@author Adam,
 */

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Vector;

import storage.SongObject;


public class RMIServerImpl extends UnicastRemoteObject implements RMIServer {

	int port = 0;


	private String name;


	private static final long serialVersionUID = 5885886202424414094L;

	public RMIServerImpl(String name) throws RemoteException {
		this.name = name;
	}

	public int getPort() throws RemoteException{
		return port;
	}

	public void registerHifi(String s) throws RemoteException{
		try{
			FileWriter fstream = new FileWriter("./src/server/hifi_loc.xml",true);
			BufferedWriter out = new BufferedWriter(fstream);
			out.write("<HIFI>"+s+"</HIFI>");
			out.close();
		}catch (Exception e){
			e.printStackTrace();
		}
	}

	public void sendMP3s(Vector v) throws RemoteException{
		System.out.println("got a list of mp3s from storage:");
		for (int i = 0; i<v.size(); i++){
			System.out.println(v.elementAt(i).toString());
			SongObject so = (SongObject) v.elementAt(i);
			try{
				FileWriter fstream = new FileWriter("./src/server/FullMp3s.xml",true);
				BufferedWriter out = new BufferedWriter(fstream);
				out.write(so.toString() + "\n");
				out.close();
			}catch (Exception e){
				e.printStackTrace();
			}
		}
	}

	public synchronized void storageLeaving(String s) throws RemoteException{
		System.out.println("storage is leaving");
		String outfile = "";
		try{
			FileInputStream fstream = new FileInputStream("./src/server/FullMp3s.xml");
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			
			while ((strLine = br.readLine()) != null)   {
				if (strLine.contains(s) == false){
					outfile = outfile + strLine + "\n";
				}
			}
			in.close();
		}catch (Exception e){
			System.err.println("Error: " + e.getMessage());
		}
		try{
			FileWriter fstream = new FileWriter("./src/server/FullMp3s.xml");
			BufferedWriter out = new BufferedWriter(fstream);
			out.write(outfile + "\n");
			out.close();
		}catch (Exception e){
			e.printStackTrace();
		}

	}
	
	public String HifiWhatToPlay() throws RemoteException{
		Playlist p = new Playlist();
		String s = p.getNextSong();
		return s;
	}

	public void begin() {
		try {
			Registry registry = LocateRegistry.getRegistry();
			registry.rebind("RMIServer", new RMIServerImpl("RMIServer"));
			port = registry.REGISTRY_PORT;
		} catch (RemoteException e) {
			System.err.println("Something happened trying to start RMI");
			e.printStackTrace();
			System.exit(-1);
		}
		System.out.println("The RMI server is ready");
	}

	public void addSongToPlaylist(String[] s) throws RemoteException {
		// TODO Auto-generated method stub
		 
	}

	public String getListOfSongs() throws RemoteException {
//		File file = new File("./FUllMp3s.xml");
//		FileInputStream fis = new FileInputStream(file);
//		BufferedInputStream bis = new BufferedInputStream(fis);
//		DataInputStream dis = new DataInputStream(bis);
//		String songs = "";
//		while (dis.available() != 0)
//		{
//			songs.concat(dis.readLine());
//		}	
//		fis.close();
//		bis.close();
//		dis.close();
//		return songs;
		return "a";
	}
}
